package com.wandousoushu.jiusen.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wandousoushu.jiusen.R;
import com.wandousoushu.jiusen.booksource.bean.BookBean;
import com.wandousoushu.jiusen.booksource.data.entities.Book;

import java.util.List;

public class BookMarkAdapter extends RecyclerView.Adapter<BookMarkAdapter.Vh>  {

    private Context mContext;
    private List<Book> mBookList;
    private LayoutInflater mInflater;
    private ActionListener mActionListener;
    private View.OnClickListener mOnClickListener;
    private BookShelfAdapter.LongClickListener mLongClickListener;
    private View.OnLongClickListener mOnLongClickListener;
    private int[] bgBookMark = {R.drawable.bg_bookshelf_item, R.drawable.bg_bookshelf_item1,
            R.drawable.bg_bookshelf_item2, R.drawable.bg_bookshelf_item3, R.drawable.bg_bookshelf_item4,
            R.drawable.bg_bookshelf_item5, R.drawable.bg_bookshelf_item6, R.drawable.bg_bookshelf_item7, R.drawable.bg_bookshelf_item8};

    public BookMarkAdapter(Context mContext, List<Book> bookList) {
        this.mContext = mContext;
        this.mBookList=bookList;
        mInflater = LayoutInflater.from(mContext);
        this.notifyDataSetChanged();
        mOnClickListener=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Object tag = view.getTag();
                if(tag!=null){
                    int position = (int) tag;
                    if (mActionListener != null) {
                        mActionListener.onItemClick(mBookList.get(position));
                    }
                }
            }
        };
        mOnLongClickListener = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Object tag = view.getTag();
                if (tag != null) {
                    int position = (int) tag;
                    if (mLongClickListener != null) {
                        mLongClickListener.onItemLongClick(mBookList.get(position));
                    }
                }
                return false;
            }
        };
    }

    public void setActionListener(ActionListener listener) {
        mActionListener = listener;
    }

    public void setLongClickListener(BookShelfAdapter.LongClickListener listener) {
        mLongClickListener = listener;
    }

    @Override
    public BookMarkAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_bookmark_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(BookMarkAdapter.Vh holder, int position) {
        holder.setData(mBookList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mBookList.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        TextView originNameTextView;
        TextView nameBigTextView;
        TextView nameTextView;
        TextView originTextView;
        LinearLayout itemLinearLayout;

        public Vh(View itemView) {
            super(itemView);
            itemLinearLayout = itemView.findViewById(R.id.bookmark_ll_item);
            originNameTextView = (TextView) itemView.findViewById(R.id.tv_bookmark_origin_name);
            nameBigTextView=(TextView)itemView.findViewById(R.id.tv_bookmark_name_big);
            nameTextView=(TextView)itemView.findViewById(R.id.tv_bookmark_name);
            originTextView=(TextView)itemView.findViewById(R.id.tv_bookmark_origin) ;
            itemView.setOnClickListener(mOnClickListener);
            itemView.setOnLongClickListener(mOnLongClickListener);
        }

        void setData(Book bean, int position) {
            itemView.setTag(position);
            itemLinearLayout.setBackgroundResource(bgBookMark[position % 9]);
            originNameTextView.setText(bean.getOriginName());
            nameBigTextView.setText(bean.getName());
            nameTextView.setText(bean.getName());
            originTextView.setText(bean.getOrigin());
        }
    }


    public void refresh(List<Book> newList) {
        mBookList.removeAll(mBookList);
        mBookList.addAll(newList);
        notifyDataSetChanged();
    }


    public interface ActionListener {
        void onItemClick(Book book);
    }

    public interface LongClickListener {
        void onItemLongClick(Book book);
    }
}
